/*******************************************************************************
 * Copyright 2011 Towee.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package net.towee.client.widgets;

import com.google.gwt.user.client.ui.FlowPanel;

public class DecoratedPanel extends FlowPanel {
	
	/**
	* The default style name.
	*/
	private static final String DEFAULT_STYLENAME = "decoratedPanel";

	/**
	 * The default styles applied to each row.
	 */
	private static final String[] DEFAULT_ROW_STYLENAMES = {"top", "middle", "bottom"};
	
	public DecoratedPanel(){
		this(DEFAULT_STYLENAME, DEFAULT_ROW_STYLENAMES, false, false);
	}
	
	public DecoratedPanel(String styleName){
		this(styleName, DEFAULT_ROW_STYLENAMES, false, false);
	}
	
	public DecoratedPanel(boolean withoutTop, boolean withoutBottom){
		this(DEFAULT_STYLENAME, DEFAULT_ROW_STYLENAMES, withoutTop, withoutBottom);
	}
	
	public DecoratedPanel(String styleName, boolean withoutTop, boolean withoutBottom){
		this(styleName, DEFAULT_ROW_STYLENAMES, withoutTop, withoutBottom);
	}

	DecoratedPanel(String styleName, String[] styleNames, boolean withoutTop, boolean withoutBottom) {
		// Add rows
		if(withoutTop == false) makeDecoratedRow(styleNames[0], withoutTop, withoutBottom); // Top row
		makeDecoratedRow(styleNames[1], withoutTop, withoutBottom); // Middle row
		if(withoutBottom == false) makeDecoratedRow(styleNames[2], withoutTop, withoutBottom); // Bottom row
		
		// Set stylename
		setStyleName(styleName);
	}
	
	private void makeDecoratedRow(String styleName, boolean withoutTop, boolean withoutBottom) {
		// Make containers
		FlowPanel center = new FlowPanel();
		FlowPanel left   = new FlowPanel();
		FlowPanel right  = new FlowPanel();
		FlowPanel cont   = new FlowPanel();
		
		// Container
		cont.addStyleName("absolute");
		cont.addStyleName(styleName+"Container");
		
		if(withoutTop    != false) cont.addStyleName("withoutTop");
		if(withoutBottom != false) cont.addStyleName("withoutBottom");
		
		//  Set left row
		left.addStyleName("absolute");
		left.addStyleName(styleName+"Left");
		
		// Set right row
		right.addStyleName("absolute");
		right.addStyleName(styleName+"Right");
		
		// Set center row
		center.addStyleName("absolute");
		center.addStyleName(styleName+"Center");

		// Add rows to container
		cont.add(center);
		cont.add(left);
		cont.add(right);
		
		// Add container to decor panel
		add(cont);		
	}
}
